package com.example.vaadin6test2;

import org.vaadin.console.Console;

import com.github.wolfie.refresher.Refresher;
import com.vaadin.Application;
import com.vaadin.ui.*;

/**
 * Main application class.
 */
public class Vaadin6test2Application extends Application {
	
	private Console console;
	
	@Override
	public void init() {
		Window mainWindow = new Window("Vaadin6test Application");
		Label label = new Label("Hello Vaadin user");
		console = new Console();
		 console.setPs("}> ");
	        console.setCols(80);
	        console.setRows(24);
	        console.setMaxBufferSize(24);
	        console.setGreeting("Welcome to Vaadin console demo.");
	        console.reset();
	        console.focus();
	       
	        Refresher refresher = new Refresher();
	        refresher.setRefreshInterval(1000);
	        mainWindow.addComponent(refresher);
	        
	       
	   Thread thread = new Thread(new Watek());
	    
	   thread.start();
		mainWindow.addComponent(label);
		mainWindow.addComponent(console);
		setMainWindow(mainWindow);
	}
	
	class Watek implements Runnable{

		
		public void run() {
			for(int i = 0; i < 10; i++){
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("Writing to console " + i);
				getConsole().getPrintStream().append(String.format("Odpowiedz %s\n", i));
				
			}
			
		}
		
	}
	
	public synchronized Console getConsole(){
		return console;
	}

}

